package jscinttools.datafit.scint;

import java.lang.StringBuilder;
import java.lang.String;

import jscinttools.datafit.scint.PulseFitFunc;

import hep.aida.IAnalysisFactory;
import hep.aida.ITreeFactory;
import hep.aida.ITree;
import hep.aida.IFunction;

/**
 *  Class to create a function defined as
 *  A0(exp(-t/tau0)-exp(-t)/tauR))+A1exp(-t/tau1)+...
 *
 *@author     Marc-Antoine Verdier
 *@created    8 avril 2011
 */
public class NExpRiseFitFunc implements PulseFitFunc
{
	/**
	 *  Setup the fit function as A0(exp(-t/tau0)-exp(-t/tauR))+A1exp(-t/tau1)+...
	 *
	 *@param  param  The parameters defining the IFunction in this order:
	 *      A0,tau0,A1,tau1,...,tauR
	 *@return        The IFunction used for fitting
	 */
	public IFunction fitFunction(double[] param)
	{
		StringBuilder sbF = new StringBuilder();
		StringBuilder sbP = new StringBuilder();
		for (int i = 0; i < (param.length - 1) / 2; i++)
		{
			if (i == 0)
			{
				sbF.append("A" + i + "*(exp(-1.0*x[0]/tau" + i + ")-exp(-1.0*x[0]/tauR))");
				sbP.append("A" + i + ",tau" + i);
			}
			else
			{
				sbF.append("+A" + i + "*exp(-1.0*x[0]/tau" + i + ")");
				sbP.append(",A" + i + ",tau" + i);
			}
		}
		sbP.append(",tauR");

		IFunction func = af.createFunctionFactory(af.createTreeFactory().create()).createFunctionFromScript("NExpRiseFitFunc", 1, sbF.toString(), sbP.toString(), "NExpRiseFitFunc");
		func.setParameters(param);
		return func;
	}
}

